package vars

import (
	"github.com/kirinlabs/utils/str"
	"regexp"
	"strings"
)

type varMgr struct {
}

func NewVarMgr() *varMgr {
	return &varMgr{}
}

func (m varMgr) ReplaceSysVar(src string) string {
	pn := "(?i)@(\\w|_|[\u4e00-\u9fa5])*@"
	reg, _ := regexp.Compile(pn)
	for _, key := range reg.FindAllString(src, -1) {
		if str.StartsWith(key, "@DATE") {
			src = strings.ReplaceAll(src, key, DateVar.GetStr(key))
		} else if str.StartsWith(key, "@C_") { //系统配置
			src = strings.ReplaceAll(src, key, ConfVar.GetStr(key))
		} else { // 部门、角色
			src = strings.ReplaceAll(src, key, OrgVar.GetStr(key))
		}
	}

	return src
}

func (m varMgr) GetOrgMap() map[string]interface{} {
	return OrgVar.GetMap()
}

func (m varMgr) GetConfMap() map[string]interface{} {
	return ConfVar.GetMap(true)
}

func (m varMgr) GetDateMap() map[string]interface{} {
	return DateVar.GetMap()
}


